package cn.edu.ecut.se2024.service;

import java.util.ArrayList;
import java.util.HashMap;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.ecut.se2024.dao.SupplierMgmtDAO;
import cn.edu.ecut.se2024.exception.OperationFailedException;
import cn.edu.ecut.se2024.model.supplier_mgmt.Supplier;
import cn.edu.ecut.se2024.model.supplier_mgmt.SupplierAddress;
import cn.edu.ecut.se2024.model.supplier_mgmt.SupplierContact;

@Service
public class SupplierMgmtService {
	
	private final SupplierMgmtDAO supplierMgmtDAO;
	
	public SupplierMgmtService(SupplierMgmtDAO supplierMgmtDAO) {
		this.supplierMgmtDAO = supplierMgmtDAO;
	}
	
	public ArrayList<Supplier> getAllSupplierInformation(){
		// 获取所有供应商、联系人、地址信息
		ArrayList<Supplier> sl = supplierMgmtDAO.getAllSupplierData();
		ArrayList<SupplierContact> scl = supplierMgmtDAO.getAllSupplierContactData();
		ArrayList<SupplierAddress> sal = supplierMgmtDAO.getAllSupplierAddressData();
		
		// 以供应商id为键，sl对应供应商id下标为值，方便索引
		HashMap<Integer, Integer> sid_index = new HashMap<>();
		for(int i = 0; i < sl.size(); i++) {
			sid_index.put(sl.get(i).getId(), i);
			// 初始化联系人列表和地址列表
			sl.get(i).setContact(new ArrayList<SupplierContact>());
			sl.get(i).setAddress(new ArrayList<SupplierAddress>());
		}
		// 设置供应商类中供应商联系人列表
		for(int i = 0; i < scl.size(); i++) {
			sl.get(sid_index.get(scl.get(i).getSid())).getContact().add(scl.get(i));
		}
		// 设置供应商类中供应商地址列表
		for(int i = 0; i < sal.size(); i++) {
			sl.get(sid_index.get(sal.get(i).getSid())).getAddress().add(sal.get(i));
		}
		
		return sl;
	}
	
	public ArrayList<Supplier> getSupplierNameInformation(String supplier_name) {
		// 获取所有供应商信息
		ArrayList<Supplier> sl = getAllSupplierInformation();
		// 筛选供应商名称
		ArrayList<Supplier> new_sl = new ArrayList<Supplier>();
		for(int i = 0; i < sl.size(); i++) {
			if(sl.get(i).getName().contains(supplier_name)) {
				new_sl.add(sl.get(i));
			}
		}
		
		return new_sl;
	}
	
	public Supplier getSpecialSupplierInformation(int supplier_id) throws OperationFailedException {
		// 获取指定供应商id的供应商信息
		Supplier s = supplierMgmtDAO.getSpecialSupplierData(supplier_id);
		ArrayList<SupplierContact> scl = supplierMgmtDAO.getSpecialSupplierContactData(supplier_id);
		ArrayList<SupplierAddress> sal = supplierMgmtDAO.getSpecialSupplierAddressData(supplier_id);
		if (s == null) {
			throw new OperationFailedException("获取供应商信息失败，不存在该条供应商信息，请尝试刷新重试！");
		}
		else {
			s.setContact(scl);
			s.setAddress(sal);
		}
		
		return s;
	}
	
	@Transactional
	public boolean addSupplierInformation(Supplier supplier) throws OperationFailedException {
		// 增加供应商信息
		boolean succeed = supplierMgmtDAO.addSupplierData(supplier);
		if(!succeed) {
			throw new OperationFailedException("增加供应商信息失败，请稍后再试！");
		}
		// 增加供应商联系人信息
		ArrayList<SupplierContact> scl = supplier.getContact();
		for(int i = 0; i < scl.size(); i++) {
			// 设置供应商ID
			scl.get(i).setSid(supplier.getId());
			succeed = supplierMgmtDAO.addSupplierContactData(scl.get(i));
			if(!succeed) {
				throw new OperationFailedException("增加供应商信息失败，请稍后再试！");
			}
		}
		// 增加供应商地址信息
		ArrayList<SupplierAddress> sal = supplier.getAddress();
		for(int i = 0; i < sal.size(); i++) {
			// 设置供应商ID
			sal.get(i).setSid(supplier.getId());
			succeed = supplierMgmtDAO.addSupplierAddressData(sal.get(i));
			if(!succeed) {
				throw new OperationFailedException("增加供应商信息失败，请稍后再试！");
			}
		}
		
		return succeed;
	}
	
	@Transactional
	public boolean updateSupplierInformation(Supplier supplier) throws OperationFailedException {
		// 修改供应商信息
		boolean succeed = supplierMgmtDAO.updateSupplierData(supplier);
		if(!succeed) {
			throw new OperationFailedException("修改供应商信息失败，请稍后再试！");
		}
		
		// 获取新的供应商联系人和地址信息
		ArrayList<SupplierContact> new_scl = supplier.getContact();
		ArrayList<SupplierAddress> new_sal = supplier.getAddress();
		// 获取旧的供应商联系人和地址信息
		Supplier old_supplier = getSpecialSupplierInformation(supplier.getId());
		ArrayList<SupplierContact> old_scl = old_supplier.getContact();
		ArrayList<SupplierAddress> old_sal = old_supplier.getAddress();
		// 对供应商联系人信息按新旧列表长度进行增删改
		int i = 0, j = 0;
		for(i = 0, j = 0; i < old_scl.size() && j < new_scl.size(); i++, j++) {
			new_scl.get(j).setId(old_scl.get(i).getId());
			succeed = supplierMgmtDAO.updateSupplierContactData(new_scl.get(j));
			if(!succeed) {
				throw new OperationFailedException("修改供应商信息失败，请稍后再试！");
			}
		}
		if(i < old_scl.size()) {
			for( ; i < old_scl.size(); i++) {
				succeed = supplierMgmtDAO.deleteSupplierContactData(old_scl.get(i).getSid(), old_scl.get(i).getId());
				if(!succeed) {
					throw new OperationFailedException("修改供应商信息失败，请稍后再试！");
				}
			}
		}
		if(j < new_scl.size()) {
			for( ; j < new_scl.size(); j++) {
				succeed = supplierMgmtDAO.addSupplierContactData(new_scl.get(j));
				if(!succeed) {
					throw new OperationFailedException("修改供应商信息失败，请稍后再试！");
				}
			}
		}
		// 对供应商地址信息按新旧列表长度进行增删改
		for(i = 0, j = 0; i < old_sal.size() && j < new_sal.size(); i++, j++) {
			new_sal.get(j).setId(old_sal.get(i).getId());
			succeed = supplierMgmtDAO.updateSupplierAddressData(new_sal.get(j));
			if(!succeed) {
				throw new OperationFailedException("修改供应商信息失败，请稍后再试！");
			}
		}
		if(i < old_sal.size()) {
			for( ; i < old_sal.size(); i++) {
				succeed = supplierMgmtDAO.deleteSupplierAddressData(old_sal.get(i).getSid(), old_sal.get(i).getId());
				if(!succeed) {
					throw new OperationFailedException("修改供应商信息失败，请稍后再试！");
				}
			}
		}
		if(j < new_sal.size()) {
			for( ; j < new_sal.size(); j++) {
				succeed = supplierMgmtDAO.addSupplierAddressData(new_sal.get(j));
				if(!succeed) {
					throw new OperationFailedException("修改供应商信息失败，请稍后再试！");
				}
			}
		}
		
		return succeed;
	}
	
	@Transactional
	public boolean deleteSupplierInformation(int supplier_id) throws OperationFailedException {
		// 删除供应商信息
		boolean succeed = supplierMgmtDAO.deleteSupplierData(supplier_id);
		if(!succeed) {
			throw new OperationFailedException("删除供应商信息失败，不存在该条供应商信息，请尝试刷新重试！");
		}
		
		return succeed;
	}
}
